package com.by.ui.recyclerview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;

import com.by.ui.R;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2016/5/11.
 */
public class RecyclerViewActivity extends AppCompatActivity {
    private ViewPager mViewPager;
    private List<View> mData;
    private LayoutInflater mInflater;
    private MyPageAdapter mAdapter;
    //
    private MyAdapter mLinearAdapter;
    private XRecyclerView mLinearRV;
    //
    private MyAdapter mGridAdapter;
    private XRecyclerView mGridRV;
    //
    private MyAdapter mStaggeredAdapter;
    private XRecyclerView mStaggeredRV;

    private Handler mHandler = new Handler();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mInflater = LayoutInflater.from(this);
        initData();
        mAdapter = new MyPageAdapter(mData);
        mViewPager.setAdapter(mAdapter);
    }

    private void initData() {
        mData = new ArrayList<>();
        initLinearView();
        initGridView();
        initStaggeredView();
    }

    private void initLinearView() {
        View view = mInflater.inflate(R.layout.page_recycler_view, null);
        mLinearRV = (XRecyclerView) view.findViewById(R.id.recycler_view);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mLinearRV.setLayoutManager(manager);
        mLinearRV.setRefreshProgressStyle(ProgressStyle.SysProgress);
        mLinearRV.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotatePulse);
        mLinearRV.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mLinearAdapter.resetData();
                        mLinearRV.refreshComplete();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mLinearAdapter.addData();
                        mLinearRV.loadMoreComplete();
                    }
                }, 2000);
            }
        });
        mLinearAdapter = new MyAdapter();
        mLinearRV.setAdapter(mLinearAdapter);
        mLinearRV.setRefreshing(true);
        mData.add(view);
    }


    private void initGridView() {
        View view = mInflater.inflate(R.layout.page_recycler_view, null);
        mGridRV = (XRecyclerView) view.findViewById(R.id.recycler_view);
        GridLayoutManager manager = new GridLayoutManager(this, 3);
        mGridRV.setLayoutManager(manager);
        mGridRV.setRefreshProgressStyle(ProgressStyle.SysProgress);
        mGridRV.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotatePulse);
        mGridRV.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mGridAdapter.resetData();
                        mGridRV.refreshComplete();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mGridAdapter.addData();
                        mGridRV.loadMoreComplete();
                    }
                }, 2000);
            }
        });
        mGridAdapter = new MyAdapter();
        mGridRV.setAdapter(mGridAdapter);
        mData.add(view);
    }

    private void initStaggeredView() {
        View view = mInflater.inflate(R.layout.page_recycler_view, null);
        mStaggeredRV = (XRecyclerView) view.findViewById(R.id.recycler_view);
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL);
        mStaggeredRV.setLayoutManager(manager);
        mStaggeredRV.setRefreshProgressStyle(ProgressStyle.SysProgress);
        mStaggeredRV.setLoadingMoreProgressStyle(ProgressStyle.BallClipRotatePulse);
        mStaggeredRV.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mStaggeredAdapter.resetData();
                        mStaggeredRV.refreshComplete();
                    }
                }, 2000);
            }

            @Override
            public void onLoadMore() {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mStaggeredAdapter.addData();
                        mStaggeredRV.loadMoreComplete();
                    }
                }, 2000);
            }
        });
        mStaggeredAdapter = new MyAdapter();
        mStaggeredRV.setAdapter(mStaggeredAdapter);


        mData.add(view);
    }

}
